const fs = require('fs')
const readline = require('readline')
/**
 *
 *
 * @param {String} filePath
 * @param {Array} targetString
 * @return {*}
 */
function readFileByLine(filePath, targetString) {
	return new Promise((resolve, reject) => {
		let fRead = fs.createReadStream(filePath)
		let objReadline = readline.createInterface({
			input: fRead,
		})
		let arr = new Array()
		objReadline.on('line', (line) => {
			arr.push(line)
		})
		let result = new Array()
		objReadline.on('close', () => {
			arr.map((line, index) => {
				targetString.map((key) => {
					if (line.indexOf(key) > -1) {
						result.push({
							path: filePath,
							lineNum: index + 1,
							content: line,
							target: key,
						})
					}
				})
			})
			resolve(result)
		})
	})
}

module.exports = {
	readFileByLine,
}
